package com.xwj.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xwj.config.FileUploadCosConfig;
import com.xwj.data.StaticData;
import com.xwj.entity.PwdInfo;
import com.xwj.entity.RaRole;
import com.xwj.entity.UserInfo;
import com.xwj.service.Impl.RaRoleServiceImpl;
import com.xwj.service.Impl.RaUserServiceImpl;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by thinkjoy on 2017/7/20.
 * 个人中心
 */
@RestController
@RequestMapping("/admin/person/")
public class PersonController {

    @Autowired
    private RaUserServiceImpl raUserService;
    @Autowired
    private RaRoleServiceImpl roleService;
    @Autowired
    private FileUploadCosConfig fileUploadCos;

    @RequestMapping("personlist")
    public ModelAndView personList(ModelAndView modelAndView, HttpServletRequest request) {
        ServletContext context = request.getServletContext();
        String accountid = (String) context.getAttribute(StaticData.ADMIN_USER_LOGIN_ATTRIBUTE);
        UserInfo userInfo = raUserService.findPropertyByAccountID(accountid);
        if (StringUtils.isEmpty(userInfo.getPhoto())) {
            userInfo.setPhoto("/static/image/photo.jpg");
        }
        RaRole raRole = roleService.findRoleById(userInfo.getRoleid());
        userInfo.setRaRole(raRole);
        modelAndView.addObject("userinfo", userInfo);
        modelAndView.setViewName("/admin/person");
        return modelAndView;
    }

    //    上传头像
    @RequestMapping(value = "uploadmyphoto", method = RequestMethod.POST)
    public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        String localPathDir = "/photo";
        String localRealPathDir = request.getSession().getServletContext()
                .getRealPath(localPathDir);
        File logoSaveFile = new File(localRealPathDir);
        if (!logoSaveFile.exists())
            logoSaveFile.mkdirs();
        /** 页面控件的文件流* */
        MultipartFile multipartFile = multipartRequest.getFile("file");
        String fileName = localRealPathDir + File.separator + file.getOriginalFilename();
        File files = new File(fileName);
        multipartFile.transferTo(files);
        String result = fileUploadCos.uploadFile("/userphoto/", fileName, file.getOriginalFilename());
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getInteger("code") == 0) {
            files.delete();
        }
        return result;
    }

    /**
     * 修改个人信息
     *
     * @return
     */
    @RequestMapping("updateperson")
    public String updatePersonData(UserInfo userInfo) {
        int row = raUserService.updateByPersonData(userInfo);
        if (row > 0) {
            return "0";
        } else {
            return "1";
        }
    }

    /**
     * 修改密码
     *
     * @param pwdInfo
     * @param request
     * @return
     */
    @RequestMapping("updatepwd")
    public String updatePwd(PwdInfo pwdInfo, HttpServletRequest request) {
        ServletContext context = request.getServletContext();
        String accountid = (String) context.getAttribute(StaticData.ADMIN_USER_LOGIN_ATTRIBUTE);
        UserInfo userInfo = raUserService.findUserByAccountID(accountid, "0");
        if (userInfo.getPassword().equals(new Md5Hash(pwdInfo.getOldpwd(), userInfo.getSalt()).toString())) {
            String slat = UUID.randomUUID().toString().substring(0, 8);
            String spwd = new Md5Hash(pwdInfo.getNewpwd(), slat).toString();
            userInfo.setSalt(slat);
            userInfo.setPassword(spwd);
            int raw = raUserService.updatepwd(userInfo);
            if (raw > 0) {
                return "0";
            } else {
                return "1";
            }
        } else {
            return "2";
        }
    }
}
